import axios from 'axios';
import { Message } from 'element-ui'
const ResultCode = {
    CODE_FAIL: -1,
    CODE_SUCCESS: 200,
    CODE_NO_MODULE: 1000,
    CODE_INVALID_INPUT: 1001,
    CODE_SESSION_EXPIRED: -1002,
    CODE_PERMISSION_DENIED: 1003,
}

const service = axios.create({
    timeout: 10000,
    baseURL: process.env.VUE_APP_BASE_API,
});

service.interceptors.request.use(
    config => {
        const token = sessionStorage.getItem("Token")
        if (token) {
            config.headers.common['token'] = token
        }
        return config;
    },
    error => {
        console.log(error);
        return Promise.reject(error);
    }
);

service.interceptors.response.use(

    response => {
        if (response.status === 200) {
            const res = response.data
            switch (res.code) {
                case ResultCode.CODE_SUCCESS:
                    return res;
                case ResultCode.CODE_FAIL:
                    Message.error(`[错误] ${res.message}`)
                    return
                default:
                    break;
            }
        } else {
            Promise.reject();
        }
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);

export default service;
